/* ------------------------------------------------------------------------
	Author: SlopeTracker (http://www.pluginexpert.com)
	Version: 1.0
------------------------------------------------------------------------- */
var array1 = new Array();
var array2 = new Array();
var stt = 0;

(function(jQuery) {
    jQuery.fn.getAttributes = function() {
        var attributes = {}; 

        if(!this.length)
            return this;

        jQuery.each(this[0].attributes, function(index, attr) {
            attributes[attr.name] = attr.value;
        }); 

        return attributes;
    }
})(jQuery);

var stFacebookToolboxcustom = {
    array1 : Array(),
    array2 : Array(),
    stt :  0,
    setting_display_form: function(id,value){
        if(jQuery('#'+id).is(':checked'))
        {
            jQuery("#"+value).slideDown("slow");
        }
        else
        {
            jQuery("#"+value).slideUp("slow");
        }
    },return_false:function(){
        return false;
    }
    ,
    update_site_map: function (id){
        var post_data = "ajax=1&action=update_site_map&id="+id;
        jQuery.ajax({
            type: "POST",
            data: post_data,
            cache: false,
            beforeSend: function(){
                jQuery("#facebook_site_map").html("<img src='"+jQuery('#picture').val()+"' /><br />Updating  ....");
            },
            success: function(data){
                stFacebookToolboxcustom.loadPage("site_map");
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                setTimeout(jQuery.unblockUI, 2000);
            }
        });
    },
    redirect: function(url){
      window.location = url;  
    },
    insert_element: function(type){
        var count = jQuery("#count").val();
        count = parseInt(count)+1;
        jQuery("#count").val(count);
        jQuery("#show_error").hide();
        jQuery("#save_form").slideDown();
        //var header = '<li class="ui-state-default" id="element_'+count+'" style="display:none;padding:10px;cursor:pointer;" ><input type="hidden" value="'+count+'" class="hidden" /><input type="hidden" value="'+type+'" id="type_'+count+'" /><input type="hidden" id="element_guidelines_'+count+'" /><div onclick="stFacebookToolboxcustom.edit_element('+count+',\''+type+'\');">';
        var header = '<div class="w465 b1px m15b po" id="element_'+count+'" onclick="stFacebookToolboxcustom.edit_element('+count+',\''+type+'\');" style="cursor:pointer;"><input type="hidden" value="'+count+'" class="hidden" /><input type="hidden" value="'+type+'" id="type_'+count+'" /><input type="hidden" id="element_guidelines_'+count+'" /><div class="n_edit"><a href="javascript:void(0);" onclick="stFacebookToolboxcustom.delete_element('+count+')"><img alt="" src="'+jQuery('#base_url').val()+'images/edit2.png"></a> <a href="javascript:void(0);" ><img alt="" src="'+jQuery('#base_url').val()+'images/edit3.png"></a></div><div class="w430 mc p10b p10t">'
        //var footer = '</div><img src="'+jQuery("#base_url").val()+'images/icons/delete.gif" onclick="stFacebookToolboxcustom.delete_element('+count+')" /></li>';
        var footer = '</div></div>';
        var body = '';
        switch(type){
            case 'text':
            {
                body = '<span id="title_'+count+'">Text</span><span id="require_title_'+count+'" style="color:red;"></span><br /><input type="text" class="width_medium" id="default_'+count+'" />';
                break;
            }
            case 'break' :
            {
                body = '<span id="title_'+count+'">Section Break</span><input type="hidden" id="break_'+count+'" value="" />';
                break;
            }
            case 'option':
            {
                body = '<span id="title_'+count+'">Multiple Matrix</span><span id="require_title_'+count+'" style="color:red;"></span>';
                body = body + '<table class="option_css" id="default_'+count+'"><tr><td>&nbsp;</td><td>Column one</td><td>Column two</td><td>Column three</td></tr>';
                body = body + '<tr><td>Row one</td><td align="center"><input type="radio" name="row_'+count+'_1"></td><td align="center"><input type="radio" name="row_'+count+'_1"></td><td align="center"><input type="radio" name="row_'+count+'_1"></td></tr>';
                body = body + '<tr><td>Row two</td><td align="center"><input type="radio" name="row_'+count+'_2"></td><td align="center"><input type="radio" name="row_'+count+'_2"></td><td align="center"><input type="radio" name="row_'+count+'_2"></td></tr>';
                body = body + '<tr><td>Row three</td><td align="center"><input type="radio" name="row_'+count+'_3"></td><td align="center"><input type="radio" name="row_'+count+'_3"></td><td align="center"><input type="radio" name="row_'+count+'_3"></td></tr>';
                body = body + '</table>';
                break;
            }
            case 'textarea':
            {
                body = '<span id="title_'+count+'">Paragraph</span><span id="require_title_'+count+'" style="color:red;"></span><br /><textarea class="width_medium" id="default_'+count+'" ></textarea>';
                break;
            }
            case 'radio':
            {
                body = '<span id="title_'+count+'">Single Choice</span><span id="require_title_'+count+'" style="color:red;"></span>';
                body = body + '<span id="radio_'+count+'" style="font-weight: lighter;color:#000;"><br /><input type="radio"  class="radio" value="First option" name="radio_'+count+'">First option';
                body = body + '<br /><input type="radio"  class="radio" value="Second option" name="radio_'+count+'">Second option';
                body = body + '<br /><input type="radio"  class="radio" value="Third option" name="radio_'+count+'">Third option';
                body = body + '</span></span>';
                break;
            }
            case 'checkbox':
            {
                body = '<span id="title_'+count+'">Checkboxes</span><span id="require_title_'+count+'" style="color:red;"></span>';
                body = body + '<span id="radio_'+count+'" style="font-weight: lighter;color:#000;"><br /><input type="checkbox"  class="radio" value="First option" name="radio_'+count+'">First option';
                body = body + '<br /><input type="checkbox"  class="radio" value="Second option" name="radio_'+count+'">Second option';
                body = body + '<br /><input type="checkbox"  class="radio" value="Third option" name="radio_'+count+'">Third option';
                body = body + '</span></span>';
                break;
            }
            case 'select':
            {
                body = '<span id="title_'+count+'">Drop Down</span><span id="require_title_'+count+'" style="color:red;"></span><br />';
                body = body + '<select id="default_'+count+'" class="width_medium"><br /><option value="First option">First option</option>';
                body = body + '<option value="Second option">Second option</option>';
                body = body + '<option value="Third option">Third option</option>';
                body = body + '</select></span>';
                break;
            }

        }
        if(jQuery("#sortable input").length == 0 ){
            var hea = '<div class="w465 b1px m15b po" id="" onclick="" style="border:none;margin-bottom:0;height:0"><div class="w430 mc p10b p10t">'
            var foo = '</div></div>';
            jQuery("#sortable").append(hea+foo);
        }
        jQuery("#sortable").append(header+body+footer);
        jQuery('#element_'+count).hide();
        jQuery('#element_'+count).slideDown();
        this.edit_element(count, type);
    },
    change_font:function(value){
        jQuery("#font_type").attr("style","font-family:'"+value+"', serif;width:300px");
    },
    test_save_form:function(){
        var post_data = "ajax=1&action=save_form&form_name="+jQuery("#form_title").val()+"&form_description="+jQuery("#form_properties p").html()+"&form_success_message="+jQuery('#properties_edit textarea').val()+"&form_redirect="+jQuery('#properties_edit input.url').val()+"&page_id="+jQuery("#page_id").val()+"&form_email="+jQuery("#form_email").val()+"&form_style="+jQuery("#form_style").val()+"&button_label="+jQuery("#button_label").val()+"&title_color="+jQuery("#color").val();
        jQuery.ajax({
            type: "POST",
            data: post_data,
            cache: false,
            beforeSend: function(){
            //jQuery("#facebook_site_map").html("<img src='"+jQuery('#picture').val()+"' /><br />Updating  ....");
            },
            success: function(data){
                       
                stFacebookToolboxcustom.save_element(data);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                setTimeout(jQuery.unblockUI, 2000);
            }
        });
    },
    save_form:function(){
        //alert('{'+json+'}');
        var element = '';
        var default_id = jQuery("#default_id").val();
        jQuery.each(jQuery("#sortable div input.hidden"), function(index,value){
            var data = new Array();
            var  option = '';
            if(jQuery("#type_"+value.value).val() != 'radio' && jQuery("#type_"+value.value).val() != 'checkbox' && jQuery("#type_"+value.value).val() != 'break' ){
                var style = jQuery("#default_"+value.value).attr('class');
                style = style.replace('width_', '');
            }
            else
            {
                style = 'medium';
            } 
            var required = 0;
            if(jQuery("#require_title_"+value.value).html() != ''){
                required = 1;
            }
            //-------------------------------------
            if(jQuery("#type_"+value.value).val()== 'radio' || jQuery("#type_"+value.value).val()== 'checkbox'){
                jQuery.each(jQuery("#radio_"+value.value+" input"), function(index2,value2){
                    var checked = 0;
                    if(value2.checked){
                        checked = 1;
                    }
                    var post_data = "ajax=1&action=save_option&form_id="+id+"&element_id="+parseInt(index+1)+"&option_id="+parseInt(index2+1)+"&option="+value2.value+"&option_is_default="+checked;
                    option = option + '"'+parseInt(index2+1)+'":{"option":"'+value2.value+'","option_is_default":"'+checked+'"},';
                });
            }
            else {
                if(jQuery("#type_"+value.value).val() == 'select'){
                    jQuery.each(jQuery("#default_"+value.value+" option"), function(index2,value2){
                        var checked = 0;
                        if(value2.selected){
                            checked = 1;
                        }
                        var post_data = "ajax=1&action=save_option&form_id="+id+"&element_id="+parseInt(index+1)+"&option_id="+parseInt(index2+1)+"&option="+value2.value+"&option_is_default="+checked;
                        option = option + '"'+parseInt(index2+1)+'":{"option":"'+value2.value+'","option_is_default":"'+checked+'"},';
                    });
                } else {
                    if(jQuery("#type_"+value.value).val() == 'option'){
                        var column = jQuery("#default_"+value.value+" tr td");
                        var max = jQuery("#default_"+value.value+" tr td").length / jQuery("#default_"+value.value+" tr").length;
                        for(var i = 1;i<max;i++){
                            var post_data = "ajax=1&action=save_matrix&form_id="+id+"&element_id="+parseInt(index+1)+"&option_id="+parseInt(i)+"&option_column="+column[i].innerHTML+"&option_row="+column[(i*max)].innerHTML;
                            option = option + '"'+parseInt(i)+'":{"option_column":"'+column[i].innerHTML+'","option_row":"'+column[(i*max)].innerHTML+'"},';
                        }
                                
                    }
                            
                }
                            
            }
            option = option.substring(0,option.length-1);
            option = '{'+option+'}';
            //-------------------------------------
            
            var horizontal = 0;
            if(jQuery("#break_"+value.value).val()){
                horizontal = 1;
            }
            element = element +  '"'+parseInt(index+1)+'":{"element_title":"'+jQuery("#title_"+value.value).html()+'","element_horizontal":'+horizontal+',"element_guidelines":"'+jQuery('#element_guidelines_'+value.value).val()+'","element_size":"'+style+'","element_is_required":"'+required+'","element_type":"'+jQuery("#type_"+value.value).val()+'","element_default_value":"'+jQuery("#default_"+value.value).val()+'","option":'+option+'},';
            
     
        //stFacebookToolboxcustom.loadPage("test", data);
        });
        element = element.substring(0,element.length-1);
        element = '{'+element+'}';
        var json = '{"form_name":"'+jQuery("#form_title").val()+'","form_id":"'+jQuery("#default_id").val()+'","form_description":"'+jQuery("#form_properties p").html()+'","has_captcha":'+jQuery("#has_captcha").val()+',"form_success_message":"'+jQuery('#form_success_message').val()+'","form_redirect":"'+jQuery('#properties_edit input.url').val()+'","page_id":"'+jQuery("#page_id").val()+'","form_email":"'+jQuery("#form_email").val()+'","email_style":"'+jQuery("#email_style").val()+'","button_label":"'+jQuery("#button_label").val()+'","title_color":"'+jQuery("#color").val()+'","background_color_setting":"'+jQuery("#background_color_setting").val()+'","text_color_setting":"'+jQuery("#text_color_setting").val()+'","submit_button_color":"'+jQuery("#submit_button_color").val()+'","submit_button_text_color":"'+jQuery("#submit_button_text_color").val()+'","font_type":"'+jQuery("#font_type").val()+'","title_font_size":"'+jQuery("#title_font_size").val()+'","text_font_size":"'+jQuery("#text_font_size").val()+'","submit_font_size":"'+jQuery("#submit_font_size").val()+'","element":'+element+'}';
        var data = new Array();
        data['json'] = json;
        stFacebookToolboxcustom.loadPage("add_new_form",data,null,function(){
             
            var post_data = "admin.php?page=stFacebookToolbox";
            jQuery.post(post_data,{
                ajax:1,
                action:'add_new_form',
                lastid:1
            },function(value){
                data = new Array();
                data['redirect'] = 1;
                data['default_id'] = value;
                stFacebookToolbox.loadPage("applications",data);
            });
        // stFacebookToolbox.loadPage("edit_form",data);
        });
       
    },
    loading:function(){
        if(jQuery("#loading").css('display') == 'none'){
            jQuery.blockUI({
                message: jQuery('#loading'),
                css: {
                    border: 'none',
                    padding: '15px',
                    backgroundColor: '#000',
                    '-webkit-border-radius': '10px',
                    '-moz-border-radius': '10px',
                    opacity: .5,
                    color: '#fff'
                }
            });
        //jQuery("#loading").fadeIn("fast");
        }
        jQuery("#inner_content").fadeOut("fast");
        setTimeout("stFacebookToolboxcustom.hideLoading()",3000);
                
    },
    hideLoading:function(){
        jQuery("#loading").fadeOut("fast");
        jQuery.unblockUI();  
        var data = Array();
        data['default_id'] = jQuery("#default_id").val();
        stFacebookToolbox.loadPage("applications",data);
    }
    ,
    edit_form:function(id){
        var post_data = "ajax=1&action=edit_form&form_name="+jQuery("#form_title").val()+"&form_description="+jQuery("#form_properties p").val()+"&form_success_message="+jQuery('#properties_edit textarea').val()+"&form_redirect="+jQuery('#properties_edit input.url').val()+"&form_id="+id+"&page_id="+jQuery("#page_id").val()+"&form_email="+jQuery("#form_email").val()+"&form_style="+jQuery("#form_style").val()+"&button_label="+jQuery("#button_label").val()+"&title_color="+jQuery("#color").val();
        ;
        //--------------------
        var element = '';
        var default_id = jQuery("#default_id").val();
        jQuery.each(jQuery("#sortable div input.hidden"), function(index,value){
            var data = new Array();
            var  option = '';
            if(jQuery("#type_"+value.value).val() != 'radio' && jQuery("#type_"+value.value).val() != 'checkbox' && jQuery("#type_"+value.value).val() != 'break' ){
                var style = jQuery("#default_"+value.value).attr('class');
                style = style.replace('width_', '');
            }
            else
            {
                style = 'medium';
            } 
            var required = 0;
            if(jQuery("#require_title_"+value.value).html() != ''){
                required = 1;
            }
            //-------------------------------------
            if(jQuery("#type_"+value.value).val()== 'radio' || jQuery("#type_"+value.value).val()== 'checkbox'){
                jQuery.each(jQuery("#radio_"+value.value+" input"), function(index2,value2){
                    var checked = 0;
                    if(value2.checked){
                        checked = 1;
                    }
                    var post_data = "ajax=1&action=save_option&form_id="+id+"&element_id="+parseInt(index+1)+"&option_id="+parseInt(index2+1)+"&option="+value2.value+"&option_is_default="+checked;
                    option = option + '"'+parseInt(index2+1)+'":{"option":"'+value2.value+'","option_is_default":"'+checked+'"},';
                });
            }
            else {
                if(jQuery("#type_"+value.value).val() == 'select'){
                    jQuery.each(jQuery("#default_"+value.value+" option"), function(index2,value2){
                        var checked = 0;
                        if(value2.selected){
                            checked = 1;
                        }
                        var post_data = "ajax=1&action=save_option&form_id="+id+"&element_id="+parseInt(index+1)+"&option_id="+parseInt(index2+1)+"&option="+value2.value+"&option_is_default="+checked;
                        option = option + '"'+parseInt(index2+1)+'":{"option":"'+value2.value+'","option_is_default":"'+checked+'"},';
                    });
                } else {
                    if(jQuery("#type_"+value.value).val() == 'option'){
                        var column = jQuery("#default_"+value.value+" tr td");
                        var max = jQuery("#default_"+value.value+" tr td").length / jQuery("#default_"+value.value+" tr").length;
                        for(var i = 1;i<max;i++){
                            // var post_data = "ajax=1&action=save_matrix&form_id="+id+"&element_id="+parseInt(index+1)+"&option_id="+parseInt(i)+"&option_column="+column[i].innerHTML+"&option_row="+column[(i*max)].innerHTML;
                            option = option + '"'+parseInt(i)+'":{"option_column":"'+column[i].innerHTML+'","option_row":"'+column[(i*max)].innerHTML+'"},';
                        }
                                
                    }
                            
                }
                            
            }
            option = option.substring(0,option.length-1);
            option = '{'+option+'}';
            //-------------------------------------
            
            var horizontal = 0;
            if(jQuery("#break_"+value.value).val()){
                horizontal = 1;
            }
            element = element +  '"'+parseInt(index+1)+'":{"element_title":"'+jQuery("#title_"+value.value).html()+'","element_horizontal":'+horizontal+',"element_guidelines":"'+jQuery('#element_guidelines_'+value.value).val()+'","element_size":"'+style+'","element_is_required":"'+required+'","element_type":"'+jQuery("#type_"+value.value).val()+'","element_default_value":"'+jQuery("#default_"+value.value).val()+'","option":'+option+'},';
            
     
        //stFacebookToolboxcustom.loadPage("test", data);
        });
        element = element.substring(0,element.length-1);
        element = '{'+element+'}';
        var json = '{"form_name":"'+jQuery("#form_title").val()+'","has_captcha":'+jQuery("#has_captcha").val()+',"form_id":"'+jQuery("#default_id").val()+'","form_description":"'+jQuery("#form_properties p").html()+'","form_success_message":"'+jQuery('#form_success_message').val()+'","form_redirect":"'+jQuery('#properties_edit input.url').val()+'","page_id":"'+jQuery("#page_id").val()+'","form_email":"'+jQuery("#form_email").val()+'","email_style":"'+jQuery("#email_style").val()+'","button_label":"'+jQuery("#button_label").val()+'","title_color":"'+jQuery("#color").val()+'","background_color_setting":"'+jQuery("#background_color_setting").val()+'","text_color_setting":"'+jQuery("#text_color_setting").val()+'","submit_button_color":"'+jQuery("#submit_button_color").val()+'","submit_button_text_color":"'+jQuery("#submit_button_text_color").val()+'","font_type":"'+jQuery("#font_type").val()+'","title_font_size":"'+jQuery("#title_font_size").val()+'","text_font_size":"'+jQuery("#text_font_size").val()+'","submit_font_size":"'+jQuery("#submit_font_size").val()+'","element":'+element+'}';
        var data = new Array();
        data['json'] = json;
        stFacebookToolboxcustom.loadPage("edit_form",data,null,function(){
            data = new Array();
            data['default_id'] = default_id;
            stFacebookToolbox.loadPage("applications",data);
        });
       
    //--------------------
    },
    save_element: function(id){
        jQuery.each(jQuery("#sortable div input.hidden"), function(index,value){
            if(jQuery("#type_"+value.value).val() != 'radio' && jQuery("#type_"+value.value).val() != 'checkbox' && jQuery("#type_"+value.value).val() != 'break' ){
                var style = jQuery("#default_"+value.value).attr('class');
                style = style.replace('width_', '');
            }
            else
            {
                style = 'medium';
            }
            var required = 0;
            if(jQuery("#require_title_"+value.value).html() != ''){
                required = 1;
            }
            var post_data = "ajax=1&action=save_element&form_id="+id+"&element_title="+jQuery("#title_"+value.value).html()+"&element_guidelines="+jQuery('#element_guidelines_'+value.value).val()+"&element_size="+style+"&element_is_required="+required+"&element_type="+jQuery("#type_"+value.value).val()+"&element_default_value="+jQuery("#default_"+value.value).val()+"&element_id="+parseInt(index+1);
            jQuery.ajax({
                type: "POST",
                data: post_data,
                cache: false,
                beforeSend: function(){
                //jQuery("#facebook_site_map").html("<img src='"+jQuery('#picture').val()+"' /><br />Updating  ....");
                },
                success: function(data){
                    if(jQuery("#type_"+value.value).val()== 'radio' || jQuery("#type_"+value.value).val()== 'checkbox'){
                        jQuery.each(jQuery("#radio_"+value.value+" input"), function(index2,value2){
                            var checked = 0;
                            if(value2.checked){
                                checked = 1;
                            }
                            var post_data = "ajax=1&action=save_option&form_id="+id+"&element_id="+parseInt(index+1)+"&option_id="+parseInt(index2+1)+"&option="+value2.value+"&option_is_default="+checked;
                            jQuery.ajax({
                                type: "POST",
                                data: post_data,
                                cache: false,
                                beforeSend: function(){
                                //jQuery("#facebook_site_map").html("<img src='"+jQuery('#picture').val()+"' /><br />Updating  ....");
                                },
                                success: function(data){
                                          
                                },
                                error: function(XMLHttpRequest, textStatus, errorThrown){
                                    setTimeout(jQuery.unblockUI, 2000);
                                }
                            });
                        });
                    }
                    else {
                        if(jQuery("#type_"+value.value).val() == 'select'){
                            jQuery.each(jQuery("#default_"+value.value+" option"), function(index2,value2){
                                var checked = 0;
                                if(value2.selected){
                                    checked = 1;
                                }
                                var post_data = "ajax=1&action=save_option&form_id="+id+"&element_id="+parseInt(index+1)+"&option_id="+parseInt(index2+1)+"&option="+value2.value+"&option_is_default="+checked;
                                jQuery.ajax({
                                    type: "POST",
                                    data: post_data,
                                    cache: false,
                                    beforeSend: function(){
                                    //jQuery("#facebook_site_map").html("<img src='"+jQuery('#picture').val()+"' /><br />Updating  ....");
                                    },
                                    success: function(data){

                                    },
                                    error: function(XMLHttpRequest, textStatus, errorThrown){
                                        setTimeout(jQuery.unblockUI, 2000);
                                    }
                                });
                            });
                        } else {
                            if(jQuery("#type_"+value.value).val() == 'option'){
                                var column = jQuery("#default_"+value.value+" tr td");
                                var max = jQuery("#default_"+value.value+" tr td").length / jQuery("#default_"+value.value+" tr").length;
                                for(var i = 1;i<max;i++){
                                    var post_data = "ajax=1&action=save_matrix&form_id="+id+"&element_id="+parseInt(index+1)+"&option_id="+parseInt(i)+"&option_column="+column[i].innerHTML+"&option_row="+column[(i*max)].innerHTML;
                                    jQuery.ajax({
                                        type: "POST",
                                        data: post_data,
                                        cache: false,
                                        beforeSend: function(){
                                        //jQuery("#facebook_site_map").html("<img src='"+jQuery('#picture').val()+"' /><br />Updating  ....");
                                        },
                                        success: function(data){

                                        },
                                        error: function(XMLHttpRequest, textStatus, errorThrown){
                                            setTimeout(jQuery.unblockUI, 2000);
                                        }
                                    });
                                }
                                
                            }
                            
                        }
                            
                    }
                       
                },
                error: function(XMLHttpRequest, textStatus, errorThrown){
                    setTimeout(jQuery.unblockUI, 2000);
                }
            });



        });
    },
    delete_form: function(id){
        jQuery( "#dialog-confirm" ).html("Are you sure you want to delete this form? This action can't be undone");
        jQuery( "#dialog-confirm" ).dialog({
            title: 'Confirm',
            resizable: false,
            modal: true,
            show: "slide",
            hide: "slide",
            buttons: [
            {
                text: "Delete",
                click: function() {
                    jQuery( this ).dialog( "close" );
                    stFacebookToolboxcustom.loadPage('delete_form&id='+id);
                    stFacebookToolboxcustom.loading();

                }
            },
            {
                text: "Cancel",
                click: function() {
                    jQuery( this ).dialog( "close" );
                }
            }
            ]
        });
    },
    delete_element: function(id){
        jQuery("#element_"+id).slideUp();
        jQuery("#element_"+id).html('');
        jQuery("#list_button").slideDown();
        jQuery("#edit_form").slideUp();
        jQuery("#form_style").slideUp();
        var delete_element = jQuery("#element_delete").val();
        if(delete_element == ''){
            delete_element = id;
        }
        else 
        {
            delete_element = delete_element + ','+id;
        }
        jQuery("#element_delete").val(delete_element);
        if(jQuery("#sortable input").length == 0){
            jQuery("#show_error").slideDown();
            jQuery("#save_form").slideUp();
        }
    },
    edit_element : function (id,type){
        if(jQuery("#element_"+id).html() == ''){
            
        }
        else{
            jQuery("#edit_form").slideUp();
            jQuery("#edit_form").slideDown();
            jQuery("#list_button").slideUp();
            jQuery("#element_type").val(type);
            jQuery("#properties_edit").slideUp();
            jQuery("#form_style").slideUp();
            jQuery("#edit_id").html(id);
            jQuery("#element_guidelines").val(jQuery("#element_guidelines_"+id).val());
            document.getElementById('field_label').focus();
            jQuery("#type").val(type);
            jQuery("#field_label").val(jQuery("#title_"+id).html());
            jQuery("#default_value").val(jQuery("#default_"+id).val());
            if(jQuery("#default_"+id).hasClass("width_medium")){
                jQuery("#field_size").val('medium');
            }else {
                if(jQuery("#default_"+id).hasClass("width_small")){
                    jQuery("#field_size").val('small');
                }
                else {
                    jQuery("#field_size").val('large');
                }
            }
            if(!(jQuery('#require_title_'+jQuery('#edit_id').html()).html() == '')){
                jQuery("#require_checkbox").attr('checked','true');

            }
            else
            {
                jQuery("#require_checkbox").removeAttr('checked');
            }
            switch(type){
                case 'radio': {
                    jQuery("#horizontal").hide();
                    jQuery("#id_no_duplicates").hide();
                    jQuery("#display_field_size").hide();
                    jQuery("#display_default_value_form").hide();
                    jQuery("#option_element").slideDown();
                    jQuery("#option_element_column").slideUp();
                    jQuery("#option_element_row").slideUp();
                    jQuery("#display_guidelines").slideDown();
                    jQuery("#display_rules").slideDown();
                    jQuery("#option_element").html('<legend style="margin-left: 6px;" >Option values</legend>');
                    var stt = 1;
                    var checked = '';
                    jQuery.each(jQuery("#radio_"+id+" input.radio"), function(index, value) {
                        // image = 'stardim';
                        checked = '';
                        if(jQuery(this).attr('checked')){
                            //image = 'star';
                            checked = 'checked';
                        }
                            
                        jQuery("#option_element").append('<input type="text" value="'+value.value+'" id="option_text_id_'+stt+'" onkeyup="stFacebookToolboxcustom.option_element('+id+',\'radio\');" style="margin-left: 34px;" /><img onclick="stFacebookToolboxcustom.insert_option_element('+id+',\'radio\');" title="Add" alt="Add" src="'+jQuery('#base_url').val()+'images/icons/add.gif" class="button"> <img onclick="stFacebookToolboxcustom.delete_option_element('+id+','+stt+',\'radio\');" title="Delete" alt="Delete" src="'+jQuery('#base_url').val()+'images/icons/delete.gif" class="button"> <input type="checkbox" onclick="stFacebookToolboxcustom.option_make_default('+id+','+stt+',\'radio\');" title="Set as on by default" alt="Set as on by default" '+checked+' class="button" style="margin-bottom:15px;"><br />');
                        stt++;
                    });

                    break;
                }
                case 'option':{
                    jQuery("#horizontal").hide();
                    jQuery("#id_no_duplicates").hide();
                    jQuery("#display_field_size").hide();
                    jQuery("#display_default_value_form").hide();
                    jQuery("#option_element").slideUp();
                    jQuery("#option_element_column").slideDown();
                    jQuery("#display_guidelines").slideDown();
                    jQuery("#display_rules").slideDown();
                    jQuery("#option_element_column").html('<legend style="margin-left: 6px;" >Option values (column) </legend>');
                    var column = jQuery("#default_"+id+" tr td");
                    var max = jQuery("#default_"+id+" tr td").length / jQuery("#default_"+id+" tr").length;
                    for(var i = 1;i<max;i++)
                        jQuery("#option_element_column").append('<input type="text" value="'+column[i].innerHTML+'" id="option_text_id_'+i+'" onkeyup="stFacebookToolboxcustom.option_element_column('+id+');" style="margin-left: 34px;" /><img onclick="stFacebookToolboxcustom.insert_option_element_column('+id+');" title="Add" alt="Add" src="'+jQuery('#base_url').val()+'images/icons/add.gif" class="button"> <img onclick="stFacebookToolboxcustom.delete_option_element_column('+id+','+i+');" title="Delete" alt="Delete" src="'+jQuery('#base_url').val()+'images/icons/delete.gif" class="button"><br />');
                    jQuery("#option_element_row").slideDown();
                    jQuery("#option_element_row").html('<legend style="margin-left: 6px;" >Option values (row)</legend>');
                    stt = 0;
                    for(var i = 1;i<jQuery("#default_"+id+" tr td").length;i++)
                        if( (i % max) == 0)
                        {
                            stt++;
                            jQuery("#option_element_row").append('<input type="text" value="'+column[i].innerHTML+'" id="option_text_id_'+stt+'" onkeyup="stFacebookToolboxcustom.option_element_column('+id+');" style="margin-left: 34px;" /><img onclick="stFacebookToolboxcustom.insert_option_element_row('+id+');" title="Add" alt="Add" src="'+jQuery('#base_url').val()+'images/icons/add.gif" class="button"> <img onclick="stFacebookToolboxcustom.delete_option_element_row('+id+','+stt+');" title="Delete" alt="Delete" src="'+jQuery('#base_url').val()+'images/icons/delete.gif" class="button"> <br />');  
                        }
                    break;
                }
                case 'checkbox': {
                    jQuery("#horizontal").hide();
                    jQuery("#id_no_duplicates").hide();
                    jQuery("#display_field_size").hide();
                    jQuery("#display_default_value_form").hide();
                    jQuery("#option_element_column").slideUp();
                    jQuery("#option_element_row").slideUp();
                    jQuery("#option_element").slideDown();
                    jQuery("#display_guidelines").slideDown();
                    jQuery("#display_rules").slideDown();
                    jQuery("#option_element").html('<legend style="margin-left: 6px;" >Option values</legend>')
                    var stt = 1;
                    jQuery.each(jQuery("#radio_"+id+" input.radio"), function(index, value) {
                        checked = '';
                        if(jQuery(this).attr('checked')){
                            checked = 'checked';
                        }
                        jQuery("#option_element").append('<input type="text" value="'+value.value+'" id="option_text_id_'+stt+'" onkeyup="stFacebookToolboxcustom.option_element('+id+',\'checkbox\');" style="margin-left: 34px;" /><img onclick="stFacebookToolboxcustom.insert_option_element('+id+',\'checkbox\');" title="Add" alt="Add" src="'+jQuery('#base_url').val()+'images/icons/add.gif" class="button"> <img onclick="stFacebookToolboxcustom.delete_option_element('+id+','+stt+',\'checkbox\');" title="Delete" alt="Delete" src="'+jQuery('#base_url').val()+'images/icons/delete.gif" class="button"> <input type="checkbox" onclick="stFacebookToolboxcustom.option_make_default('+id+','+stt+',\'checkbox\');" '+checked+' title="Set as on by default" alt="Set as on by default"  class="button" style="margin-bottom:15px;"><br />');
                        stt++;
                    });

                    break;
                }
                case 'text': {
                    jQuery("#horizontal").hide();
                    jQuery("#id_no_duplicates").slideDown();
                    jQuery("#display_field_size").slideDown();
                    jQuery("#option_element").slideUp();
                    jQuery("#option_element_column").slideUp();
                    jQuery("#option_element_row").slideUp();
                    jQuery("#display_default_value_form").slideDown();
                    jQuery("#display_guidelines").slideDown();
                    jQuery("#display_rules").slideDown();
                    break;
                }
                case 'break': {
                    jQuery("#horizontal").slideDown();
                    jQuery("#display_field_size").slideUp();
                    jQuery("#option_element").slideUp();
                    jQuery("#option_element_column").slideUp();
                    jQuery("#option_element_row").slideUp();
                    jQuery("#display_default_value_form").slideUp();
                    jQuery("#display_guidelines").slideUp();
                    jQuery("#display_rules").slideUp();
                    if(jQuery("#break_"+id).val() == '1'){
                        jQuery("#horizontal_line").attr('checked','checked');
                    }
                    else 
                    {
                        jQuery("#horizontal_line").removeAttr('checked');
                    }
                    break;
                }
                case 'textarea': {
                    jQuery("#horizontal").hide();
                    jQuery("#id_no_duplicates").slideDown();
                    jQuery("#display_field_size").slideDown();
                    jQuery("#option_element").slideUp();
                    jQuery("#option_element_column").slideUp();
                    jQuery("#option_element_row").slideUp();
                    jQuery("#display_default_value_form").slideDown(); 
                    jQuery("#display_default_value_form").slideDown();
                    jQuery("#display_guidelines").slideDown();
                    jQuery("#display_rules").slideDown();
                    break;
                }
                case 'select': {
                    jQuery("#horizontal").hide();
                    jQuery("#id_no_duplicates").hide();
                    jQuery("#display_default_value_form").hide();
                    jQuery("#option_element").slideDown();
                    jQuery("#option_element_column").slideUp();
                    jQuery("#display_field_size").slideDown();
                    jQuery("#option_element_row").slideUp();
                    jQuery("#display_guidelines").slideDown();
                    jQuery("#display_rules").slideDown();
                    jQuery("#option_element").html('<legend style="margin-left: 6px;" >Option values</legend>');
                    var stt = 1;
                    jQuery.each(jQuery("#default_"+id+" option"), function(index, value) {
                        selected = '';
                        if(jQuery(this).attr('selected')){
                            selected = 'checked';
                        }
                        jQuery("#option_element").append('<input type="text" value="'+value.value+'" id="option_text_id_'+stt+'" onkeyup="stFacebookToolboxcustom.option_element('+id+',\'select\');" style="margin-left: 34px;" /><img onclick="stFacebookToolboxcustom.insert_option_element('+id+',\'select\');" title="Add" alt="Add" src="'+jQuery('#base_url').val()+'images/icons/add.gif" class="button"> <img onclick="stFacebookToolboxcustom.delete_option_element('+id+','+stt+',\'select\');" title="Delete" alt="Delete" src="'+jQuery('#base_url').val()+'images/icons/delete.gif" class="button"> <input type="checkbox" onclick="stFacebookToolboxcustom.option_make_default('+id+','+stt+',\'select\');" title="Set as on by default" '+selected+' alt="Set as on by default" class="button" style="margin-bottom:15px;"><br />');
                        stt++;
                    });
                    break;

                }
            }
            jQuery("#last_edit").val(id);
        }
        
    },
    option_element_column : function(id){
        jQuery("#default_"+id).html('');
        var column = jQuery("#option_element_column input");
        var row    = jQuery("#option_element_row input");
        var column_string = "<tr><td>&nbsp;</td>";
        for(var i=0;i<column.length;i++)
            column_string = column_string + "<td>"+column[i].value+"</td>";
        jQuery("#default_"+id).append(column_string+"</tr>");
        for(var i=0;i<row.length;i++){
            var row_string = "<tr><td>"+row[i].value+"</td>";
            for(var a=0;a<column.length;a++)
                row_string = row_string + "<td><input type='radio' name='row_"+id+"_"+(i+1)+"'></td>";
            jQuery("#default_"+id).append(row_string+"</tr>");
            row_string = '';
        }
            
        
    }
    ,
    option_element:function(id,type){
        if(type == 'select'){
            jQuery("#default_"+id).html('');
            jQuery.each(jQuery("#option_element input[type='text']"), function(index, value) {
                jQuery("#default_"+id).append('<option value="'+value.value+'">'+value.value+'</option>');
            });
        }
        else {
            jQuery("#radio_"+id).html('');
            jQuery.each(jQuery("#option_element input[type='text']"), function(index, value) {
                jQuery("#radio_"+id).append('<br /><input type="'+type+'"  class="radio" value="'+value.value+'" name="'+type+'_'+id+'">'+value.value);
            });
        }
    },
    insert_option_element: function(id,type){
        if(type == 'select'){
            var stt = jQuery("#default_"+id+" option").length;
            jQuery("#default_"+id).html('');
            stt = parseInt(stt) + 1;
            stt = stt.toString();
            jQuery("#option_element").append('<input type="text" value="" id="option_text_id_'+stt+'" onkeyup="stFacebookToolboxcustom.option_element('+id+',\''+type+'\');" style="margin-left: 34px;" /><img onclick="stFacebookToolboxcustom.insert_option_element('+id+',\''+type+'\');" title="Add" alt="Add" src="'+jQuery('#base_url').val()+'images/icons/add.gif" class="button"> <img onclick="stFacebookToolboxcustom.delete_option_element('+id+','+stt+',\''+type+'\');" title="Delete" alt="Delete" src="'+jQuery('#base_url').val()+'images/icons/delete.gif" class="button"> <input type="checkbox" onclick="stFacebookToolboxcustom.option_make_default('+id+','+stt+',\''+type+'\');" title="Set as on by default" '+selected+' alt="Set as on by default" class="button" style="margin-bottom:15px;"><br />');
        }
        else{
            var stt = jQuery("#radio_"+id+" input.radio").length;
            jQuery("#radio_"+id).html('');
            stt = parseInt(stt) + 1;
            stt = stt.toString();
            jQuery("#option_element").append('<input type="text" value="" id="option_text_id_'+stt+'" onkeyup="stFacebookToolboxcustom.option_element('+id+',\''+type+'\');" style="margin-left: 34px;" /><img onclick="stFacebookToolboxcustom.insert_option_element('+id+',\''+type+'\');" title="Add" alt="Add" src="'+jQuery('#base_url').val()+'images/icons/add.gif" class="button"> <img onclick="stFacebookToolboxcustom.delete_option_element('+id+','+stt+',\''+type+'\');" title="Delete" alt="Delete" src="'+jQuery('#base_url').val()+'images/icons/delete.gif" class="button"> <input type="checkbox" onclick="stFacebookToolboxcustom.option_make_default('+id+','+stt+',\''+type+'\');" title="Set as on by default"  alt="Set as on by default" class="button" style="margin-bottom:15px;"><br />');
        }
        this.option_element(id,type);
    },
    insert_option_element_column :function(id){
        var stt = jQuery("#option_element_column input").length;
        jQuery("#default_"+id).html('');
        stt = parseInt(stt) + 1;
        stt = stt.toString();
        jQuery("#option_element_column").append('<input type="text" value="" id="option_text_id_'+stt+'" onkeyup="stFacebookToolboxcustom.option_element_column('+id+');" style="margin-left: 34px;" /><img onclick="stFacebookToolboxcustom.insert_option_element_column('+id+');" title="Add" alt="Add" src="'+jQuery('#base_url').val()+'images/icons/add.gif" class="button"> <img onclick="stFacebookToolboxcustom.delete_option_element_column('+id+','+stt+');" title="Delete" alt="Delete" src="'+jQuery('#base_url').val()+'images/icons/delete.gif" class="button"><br />');
        this.option_element_column(id);
    },
    insert_option_element_row :function(id){
        var stt = jQuery("#option_element_row input").length;
        jQuery("#default_"+id).html('');
        stt = parseInt(stt) + 1;
        stt = stt.toString();
        jQuery("#option_element_row").append('<input type="text" value="" id="option_text_id_'+stt+'" onkeyup="stFacebookToolboxcustom.option_element_column('+id+');" style="margin-left: 34px;" /><img onclick="stFacebookToolboxcustom.insert_option_element_row('+id+');" title="Add" alt="Add" src="'+jQuery('#base_url').val()+'images/icons/add.gif" class="button"> <img onclick="stFacebookToolboxcustom.delete_option_element_row('+id+','+stt+');" title="Delete" alt="Delete" src="'+jQuery('#base_url').val()+'images/icons/delete.gif" class="button"><br />');
        this.option_element_column(id);
    },
    option_make_default:function(id,stt,type){
        if(type == 'select'){
            jQuery("#default_"+id).html('');
            jQuery.each(jQuery("#option_element input[type='text']"), function(index, value) {
                var selected = '';
                if((index+1) == stt){
                    selected = 'selected="true"';
                }
                jQuery("#default_"+id).append('<option value="'+value.value+'" '+selected+' >'+value.value+'</option>');
            });
        }
        else
        {
            if(type=='radio'){
                jQuery("#radio_"+id).html('');
         
                jQuery.each(jQuery("#option_element input[type='text']"), function(index, value) {
                    var checked = '';
                    if((index+1) == stt){
                        checked = 'checked';
                    }
                    //jQuery("#radio_"+id+" input.radio[value='"+value.value+"']").attr('checked',checked);
                    jQuery("#radio_"+id).append('<br /><input type="'+type+'" class="radio" value="'+value.value+'" name="'+type+'_'+id+'" '+checked+'>'+value.value);
                });
            }
            else {
                jQuery.each(jQuery("#option_element input[type='text']"), function(index, value) {
                    if((index+1) == stt){
                        if(jQuery("#radio_"+id+" input.radio[value='"+value.value+"']").attr('checked')){
                            jQuery("#radio_"+id+" input.radio[value='"+value.value+"']").removeAttr('checked');
                        
                        }
                        else {
                            jQuery("#radio_"+id+" input.radio[value='"+value.value+"']").attr('checked','checked');
                        }
                        

                    }
                
                });
            }
        }
        this.edit_element(id, type);
    },
    delete_option_element_column: function (id,number){
        if(jQuery("#option_element_column input").length > 1){
            jQuery("#default_"+id).html('');
            var stt = 1;
            var column = jQuery("#option_element_column input");
            jQuery("#option_element_column").html("Option values (column) <br />");
            jQuery.each(column, function(index, value) {
                if((parseInt(index)+1) != number){
                    jQuery("#option_element_column").append('<input type="text" value="'+value.value+'" id="option_text_id_'+stt+'" onkeyup="stFacebookToolboxcustom.option_element_column('+id+');" style="margin-left: 34px;" /><img onclick="stFacebookToolboxcustom.insert_option_element_column('+id+');" title="Add" alt="Add" src="'+jQuery('#base_url').val()+'images/icons/add.gif" class="button"> <img onclick="stFacebookToolboxcustom.delete_option_element_column('+id+','+stt+');" title="Delete" alt="Delete" src="'+jQuery('#base_url').val()+'images/icons/delete.gif" class="button"><br />');
                    stt = stt+1;
                }
            });
            this.option_element_column(id);
            
        }
        else
        {
            alert("You cannot delete all choices.");
        }
    },
    delete_option_element_row: function (id,number){
        if(jQuery("#option_element_row input").length > 1){
            jQuery("#default_"+id).html('');
            var stt = 1;
            var column = jQuery("#option_element_row input");
            jQuery("#option_element_row").html("Option values (row) <br />");
            jQuery.each(column, function(index, value) {
                if((parseInt(index)+1) != number){
                    jQuery("#option_element_row").append('<input type="text" value="'+value.value+'" id="option_text_id_'+stt+'" onkeyup="stFacebookToolboxcustom.option_element_column('+id+');" style="margin-left: 34px;" /><img onclick="stFacebookToolboxcustom.insert_option_element_row('+id+');" title="Add" alt="Add" src="'+jQuery('#base_url').val()+'images/icons/add.gif" class="button"> <img onclick="stFacebookToolboxcustom.delete_option_element_row('+id+','+stt+');" title="Delete" alt="Delete" src="'+jQuery('#base_url').val()+'images/icons/delete.gif" class="button"><br />');
                    stt = stt+1;
                }
            });
            this.option_element_column(id);
            
        }
        else
        {
            alert("You cannot delete all choices.");
        }
    },
    delete_option_element: function (id,stt,type){
        if(type == 'select'){
            if(jQuery("#option_element input[type='text']").length > 1){
                jQuery("#default_"+id).html('');
                jQuery.each(jQuery("#option_element input[type='text']"), function(index, value) {
                    if((parseInt(index)+1) != stt)
                        jQuery("#default_"+id).append('<option value="'+value.value+'" >'+value.value+'</option>');
                });
                var stt = 1;
                jQuery("#option_element").html("Default value <br />")
                jQuery.each(jQuery("#default_"+id+" option"), function(index, value) {
                    jQuery("#option_element").append('<input type="text" value="'+value.value+'" id="option_text_id_'+stt+'" onkeyup="stFacebookToolboxcustom.option_element('+id+',\''+type+'\');" style="margin-left: 34px;" /><img onclick="stFacebookToolboxcustom.insert_option_element('+id+',\''+type+'\');" title="Add" alt="Add" src="'+jQuery('#base_url').val()+'images/icons/add.gif" class="button"> <img onclick="stFacebookToolboxcustom.delete_option_element('+id+','+stt+',\''+type+'\');" title="Delete" alt="Delete" src="'+jQuery('#base_url').val()+'images/icons/delete.gif" class="button"> <input type="checkbox" onclick="stFacebookToolboxcustom.option_make_default('+id+','+stt+',\''+type+'\');" title="Set as on by default" alt="Set as on by default"  class="button"  style="margin-bottom:15px;"><br />');
                    stt++;
                });
            }
            else
            {
                alert("You cannot delete all choices.");
            }
        }
        else {
            if(jQuery("#option_element input[type='text']").length > 1){
                jQuery("#radio_"+id).html('');
                jQuery.each(jQuery("#option_element input[type='text']"), function(index, value) {
                    if((parseInt(index)+1) != stt)
                        jQuery("#radio_"+id).append('<br /><input type="'+type+'"  class="radio" value="'+value.value+'">'+value.value);
                });
                //
                var stt = 1;
                jQuery("#option_element").html("Default value <br />")
                jQuery.each(jQuery("#radio_"+id+" input.radio"), function(index, value) {
                    jQuery("#option_element").append('<input type="text" value="'+value.value+'" id="option_text_id_'+stt+'" onkeyup="stFacebookToolboxcustom.option_element('+id+',\''+type+'\');" style="margin-left: 34px;" /><img onclick="stFacebookToolboxcustom.insert_option_element('+id+',\''+type+'\');" title="Add" alt="Add" src="'+jQuery('#base_url').val()+'images/icons/add.gif" class="button"> <img onclick="stFacebookToolboxcustom.delete_option_element('+id+','+stt+',\''+type+'\');" title="Delete" alt="Delete" src="'+jQuery('#base_url').val()+'images/icons/delete.gif" class="button"> <input type="checkbox" onclick="stFacebookToolboxcustom.option_make_default('+id+','+stt+',\''+type+'\');" title="Set as on by default" alt="Set as on by default"  class="button" style="margin-bottom:15px;"><br />');
                    stt++;
                });
            }
            else
            {
                alert("You cannot delete all choices.");
            }
        }
        if(jQuery("#option_delete").val() == ''){
            jQuery("#option_delete").val(id+'-'+stt);
        }
        else
        {
            jQuery("#option_delete").val(jQuery("#option_delete").val()+','+id+'-'+stt);
        }
    },
    set_size : function (value){
        var id = jQuery("#edit_id").html();
        jQuery("#default_"+id).attr('class','width_'+value);
    }
    ,
    delete_facebook_map: function(id){
        var post_data = "ajax=1&action=facebook_image_map&id="+id;
        jQuery.ajax({
            type: "POST",
            data: post_data,
            cache: false,
            beforeSend: function(){
            //jQuery("#facebook_site_map").html("<img src='"+jQuery('#picture').val()+"' /><br />Updating  ....");
            },
            success: function(data){
                stFacebookToolboxcustom.loadPage("facebook_image_map");
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                setTimeout(jQuery.unblockUI, 2000);
            }
        });
    },
    user_default_data : function(){
        jQuery("#title2").val(jQuery("#setting_title").val());
    },
    open_linter: function(){
        window.open('http://developers.facebook.com/tools/lint/?url='+jQuery("#linter").val());
    },
    loadPage: function(action, data_post, message, callback_function) {        
        var post_data = "ajax=1&action="+action;
        if(data_post != '' || data_post != null){
            for( var key in data_post){
                if(key == 'sortKey')
                    post_data +="&sort="+data_post[key];
                else
                    post_data +="&"+key+"="+data_post[key];
            }
        }
        //alert(post_data);
        jQuery.ajax({
            type: "POST",
            //url: "admin.php?page=home",
            data: post_data,
            cache: false,
            beforeSend: function(){
                if(jQuery("#loading").css('display') == 'none'){
                    jQuery.blockUI({
                        message: jQuery('#loading'),
                        css: {
                            border: 'none',
                            padding: '15px',
                            backgroundColor: '#000',
                            '-webkit-border-radius': '10px',
                            '-moz-border-radius': '10px',
                            opacity: .5,
                            color: '#fff'
                        }
                    });
                //jQuery("#loading").fadeIn("fast");
                }
                jQuery("#inner_content").fadeOut("fast");
            }, //show loading just when link is clicked
            complete: function(){
                jQuery("#loading").fadeOut("fast");
                jQuery.unblockUI();
                if(action == 'applications'){
                    var string = jQuery("#default").val()
                    id = parseInt(string);
                    if(id > 0){
                        jQuery( "#accordion" ).accordion({
                            autoHeight: false,
                            navigation: true,
                            collapsible: true,
                            active: id
                        }); 
                    }
                    else
                    {
                        jQuery( "#accordion" ).accordion({
                            autoHeight: false,
                            navigation: true,
                            collapsible: true
                        }); 
                    }
                    
                }
            }, //stop showing loading when the process is complete
            success: function(data){
                jQuery("#inner_content").html(data);
                jQuery("#inner_content").append('<input type="hidden" value="'+action+'" id="current_action" />');
                if(message){
                    stFacebookToolbox.sendMessage(message);
                }
                if(callback_function){
                    callback_function.call(this, data_post);
                }
                jQuery("#inner_content").fadeIn("normal");
                if(jQuery("#check_chart").val() == 1){
                    drawVisualization();
                }
                

                
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                setTimeout(jQuery.unblockUI, 2000);
            }
        });                
        stFacebookToolbox.pagingCallbackFunction = function(data) {};
        stFacebookToolbox.sortCallbackFunction = function(data) {};
    },
    sortColumn: function(value){
        jQuery(document).ready(function(){
            jQuery('th.manage-column').css('cursor', 'pointer');
            jQuery('th.manage-column').click(function(){
                var current_action = jQuery('#current_action').val();
                var sort_type = jQuery(this).attr('sort_type');
                var sort_id = jQuery(this).attr('id');
                if(sort_type == null){
                    sort_type = 'desc';
                }
                var data_post = new Array();

                stFacebookToolbox.sortCallbackFunction(data_post);


                data_post['sortKey'] = sort_id;
                data_post['sort_type'] = sort_type;
                jQuery('#st_pagination ul li').each(function(){
                    if(jQuery(this).hasClass('current')){
                        data_post['cur_page'] = jQuery(this).children().html();
                    }
                });
                if(value != sort_id){
                    stFacebookToolbox.loadPage(current_action, data_post, '', function(data_post){
                        if(data_post['sort_type'] == '' || data_post['sort_type'] == 'desc'){
                            jQuery('#'+data_post['sortKey']).attr('sort_type','asc');
                            jQuery('#'+data_post['sortKey']).append('<span class="icon_desc">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>');
                        }else{
                            jQuery('#'+data_post['sortKey']).attr('sort_type','desc');
                            jQuery('#'+data_post['sortKey']).append('<span class="icon_asc">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>');
                        }
                    });
                }
            });
        });
    },
    update_search: function(){
        var json = jQuery.parseJSON(jQuery("#list_page").val());
        for (i in json)
        {
            array1[stt] = i;
            array2[stt] = json[i];
            stt++;
        }
        i=0;
        stFacebookToolboxcustom.facebook_search2(0,array1,array2);
    },
    facebook_search2 : function (i,data1,data2)
    {
        var page = data2[i];
        var id   = data1[i];
        var post_data = "ajax=1&action=update_search&search="+id;
        jQuery.ajax({
            type: "POST",
            data: post_data,
            cache: false,
            beforeSend: function(){
                jQuery("#facebook_search").html("<img src='"+jQuery('#picture').val()+"' /><br />Searching for "+page+"  ....");
            },
            success: function(data){
                if(i < (stt-1))
                    stFacebookToolboxcustom.facebook_search2(parseInt(i)+1,data1,data2);
                else
                {
                    stt = 0;
                    array1 = new Array();
                    array2 = new Array();
                    stFacebookToolboxcustom.loadPage('facebook_search');
                }



            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                setTimeout(jQuery.unblockUI, 2000);
            }
        });
    },
    current : function(id)
    {
        var function_name = jQuery("#"+id).attr('id');
        jQuery('#stcontainer ul.menu_right li').each(function(){
            var current_function = jQuery(this).children().attr('id');
            if(current_function == function_name ){

                jQuery(this).addClass('current');
            }else{
                jQuery(this).removeClass('current');
            }
        });
        jQuery("#feed_result").html("<center><img src='"+jQuery("#picture").val()+"' /></center>");
    },
    get_fan_list: function(){
        var post_data = "ajax=1&action=get_fan_list&node_id="+jQuery("#node_id").val()+"&post_form_id="+jQuery("#post_form_id").val()+"&fb_dtsg="+jQuery("#fb_dtsg").val()+"&type="+jQuery("#type").val()+"&cookie="+jQuery("#cookie").val()+"&pageid="+jQuery("#pageid").val();
        jQuery.ajax({
            type: "POST",
            data: post_data,
            cache: false,
            beforeSend: function(){
                jQuery("#facebook_get_fan_list").html("<img src='"+jQuery('#picture').val()+"' /><br />Searching for  page ....");
            },
            success: function(data){
                if(jQuery('#get_fan_now').is(':checked'))
                {
                    if(parseInt(data) > 0)
                    {
                        facebook_like_get_user_info(data,0,jQuery("#pageid").val());
                    }
                    else
                    {
                        stFacebookToolbox.sendMessage("Update fail");
                    }
                }
                else
                {
                    stFacebookToolbox.loadPage("get_fan_list&pageid="+jQuery("#pageid").val(), "", "Saved Success");
                }

            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                setTimeout(jQuery.unblockUI, 2000);
            }
        });
    },
    update_user_info : function (){
        jQuery("#update_user_info").html("<img src='"+jQuery('#picture').val()+"' /><br />updating ....");
        var post_data = "ajax=1&action=user_list&search=1";
        jQuery.ajax({
            type: "POST",
            data: post_data,
            success: function(data){
                stFacebookToolboxcustom.loadPage("user_list");
            }
        });
    },
    update_statistics : function(step){
        var total = jQuery("#total").val();
        if(!(step)){
            step=0;
        }
        var id = jQuery("#site_id").val().split(',')[step];
        //alert(id);
        if(total > 0 ){
            jQuery("#update_statistics").html("<img src='"+jQuery('#picture').val()+"' /><br />updating ....");
            var post_data = "ajax=1&action=like_analytics&start="+id;
            jQuery.ajax({
                type: "POST",
                data: post_data,
                success: function(data){
                    step++;
                    if(step < total)
                        stFacebookToolboxcustom.update_statistics(step);
                    else
                        stFacebookToolboxcustom.loadPage('like_analytics','','Updated success');
                }
            });
        }
        else
        {
            stFacebookToolbox.sendMessage("No URL to update");
        }
    },
    prev_month: function(action){
        var month = jQuery("#month").val();
        var year = jQuery("#year").val();
        month = parseInt(month,10);
        year = parseInt(year,10);
        if(month == 1)
        {
            year = year - 1;
            month = 12;
        }
        else
        {
            month = month - 1;
        }
        if(month < 10)
        {
            month = '0'+month;
        }
        stFacebookToolboxcustom.loadPage(action+"&month="+month+"&year="+year);
    },
    next_month: function(action){
        var month = jQuery("#month").val();
        var year = jQuery("#year").val();
        month = parseInt(month,10);
        year = parseInt(year,10);
        if(month == 12)
        {
            year = year + 1;
            month = 1;
        }
        else
        {
            month = month + 1;
        }
        if(month < 10)
        {
            month = '0'+month;
        }
        stFacebookToolboxcustom.loadPage(action+"&month="+month+"&year="+year);
    },
    search_result_check: function(id,check){
        if(check){
            if(jQuery("#search_result_check").val() == '')
            {
                jQuery("#search_result_check").val("'"+id+"'");
            }
            else
            {
                var data = jQuery("#search_result_check").val()+"-"+"'"+id+"'";
                jQuery("#search_result_check").val(data);
            }
        		
            var string = jQuery("#search_result_check_normal").val();
            var check = true;
            for(i in  string.split(','))
            {
                id = string.split(',')[i].replace("'","");
                if(!(jQuery("#checkbox_"+id.replace("'","")).is(':checked'))){
                    check = false;
                }
                                
            }
            if(check == true)
                jQuery("#checkbox_all").attr("checked",true);
        }
        else{
            var data = jQuery("#search_result_check").val();
            data = data.replace("-"+"'"+id+"'","");
            data = data.replace("'"+id+"'"+"-","");
            data = data.replace("'"+id+"'","");
            jQuery("#search_result_check").val(data);
            jQuery("#checkbox_all").attr("checked",false);
        }
    },
    search_result_check_all: function(checked){
        if(checked){
            jQuery("#search_result_check").val(jQuery("#search_result_check_normal").val());
            var string = jQuery("#search_result_check_normal").val();
            for(i in  string.split(','))
            {
                id = string.split(',')[i].replace("'","");
                jQuery("#checkbox_"+id.replace("'","")).attr('checked', true);
            }
        }
        else
        {
            jQuery("#search_result_check").val('');
            var string = jQuery("#search_result_check_normal").val();
            for(i in  string.split(','))
            {
                id = string.split(',')[i].replace("'","");
                jQuery("#checkbox_"+id.replace("'","")).attr('checked', false);
            }
        }
    },
    action: function(value){
        if(value == 'select'){
            document.getElementById('flag_type_span').style.display = 'none';
        }
        else
        {
            if(value == 'delete')
            {
                if(jQuery("#search_result_check").val() == '')
                {
                    jQuery("#delete").val('select');
                    alert("Please choose result to delete");
                }
                else
                {
                    stFacebookToolbox.loadPage("search_results&delete=true&id="+jQuery("#search_result_check").val());
                }
            }
            else
            {
                if(value == 'apply'){
                    if(jQuery("#search_result_check").val() == '')
                    {
                        jQuery("#delete").val('select');
                        alert("Please choose result to apply flag");
                    }
                    else
                    {
                        document.getElementById('flag_type_span').style.display = 'inline';
                    }
                }
                else{
                    if(jQuery("#search_result_check").val() == '')
                    {
                        jQuery("#delete").val('select');
                        alert("Please choose result to remove flag");
                    }
                    else
                    {
                        stFacebookToolbox.loadPage("search_results&remove_flag=true&id="+jQuery("#search_result_check").val());
                    }
                }
            }
        }
    },
    add_dashboard : function (){
        //                var data_post = new Array();
        //                data_post['1'] = '1';
        //            	stFacebookToolbox.confirmModal(jQuery("#display_add_dashboard").html(), "Add",data_post,function(data){
        //                    alert(data);
        //                });
        jQuery("#box_dashboard").val(jQuery("#box").val());
        data = jQuery("#display_add_dashboard").html()
        jQuery( "#dialog-confirm" ).html(data);
        jQuery( "#dialog-confirm" ).dialog({
            title: 'Confirm',
            resizable: false,
            modal: true,
            show: "slide",
            hide: "slide",
            buttons: [
            {
                text: "Add",
                click: function() {
                    jQuery( this ).dialog( "close" );
                    var data_post = new Array();
                    data_post['box'] = jQuery('#box_dashboard').val();
                    stFacebookToolbox.loadPage('dashboard',data_post,null,function(){
                        stFacebookToolbox.loadPage("dashboard");
                    });
                        
                }
            },
            {
                text: "Cancel",
                click: function() {
                    jQuery( this ).dialog( "close" );
                }
            }
            ]
        });
    },
        
    on_submitform_new: function(form, callback_function){
        jQuery(document).ready(function(){
            jQuery('.stform input').each(function(){
                jQuery(this).focus(function(){
                    jQuery(this).parent().addClass('focus');
                });
                jQuery(this).blur(function(){
                    jQuery(this).parent().removeClass('focus');
                });
            });
            jQuery('.stform textarea').each(function(){
                jQuery(this).focus(function(){
                    jQuery(this).parent().addClass('focus');
                });
                jQuery(this).blur(function(){
                    jQuery(this).parent().removeClass('focus');
                });
            });
            jQuery('.stform select').each(function(){
                jQuery(this).focus(function(){
                    jQuery(this).parent().addClass('focus');
                });
                jQuery(this).blur(function(){
                    jQuery(this).parent().removeClass('focus');
                });
            });
            jQuery(form).validate({
                errorPlacement: function(error, element) {
                    //error.appendTo( element.parent());
                    element.parent().addClass('error');
                },
                submitHandler: function() {
                    jQuery.blockUI({
                        message: jQuery('#loading'),
                        css: {
                            border: 'none',
                            padding: '15px',
                            backgroundColor: '#000',
                            '-webkit-border-radius': '10px',
                            'border-radius': '10px',
                            '-moz-border-radius': '10px',
                            opacity: .5,
                            color: '#fff',
                            'z-index': '999999'
                        }
                    });
                    //jQuery("#loading").fadeIn("fast");
                    //jQuery("#inner_content").fadeOut("fast");
                    jQuery.post(jQuery(form).attr('action')+'&ajax=1', jQuery(form).serialize(),function(data, textStatus){
                        if(jQuery.isFunction(callback_function)){
                            callback_function.call(this, data);
                        }
                        jQuery('.stExtra').remove();
                        jQuery('#stcontainer').append('<div class="stExtra"><div style="display:none">'+data+'</div></div>');
                        if(jQuery("#loading").css('display') != 'none'){
                            jQuery.unblockUI();
                            jQuery("#loading").fadeOut('normal');
                        }
                    });
                }

            });
        });
    },

    switchButton: function(default_current_action, callbackFunction){
        jQuery(document).ready( function(){
            jQuery(".switch_checkbox").click(function(){
                if(default_current_action == null){
                    var current_action = jQuery('#current_action').val();
                }else{
                    current_action = default_current_action;
                }
                
                var arr_attribute = jQuery(this).getAttributes();
                var data = new Array;
                var post_data = "ajax=1&action="+current_action;
                

                var ajax_url = location.href;
                for(i in arr_attribute){                    
                    if(i.match('data_') != null){
                        key = i.replace('data_', '');
                        data[key] = jQuery(this).attr(i);
                    }
                } 
                
                if(jQuery(this).hasClass('cb-enable')){
                    jQuery(this).removeClass('cb-enable');
                    jQuery(this).addClass('cb-disable');
                    jQuery(this).fadeOut('fast', function(){
                        jQuery(this).css('backgroundPosition', '0');
                        jQuery(this).fadeIn('slow');
                                
                    });
                    data['featured'] = 0;
                }else if(jQuery(this).hasClass('cb-disable')){
                    jQuery(this).removeClass('cb-disable');
                    jQuery(this).addClass('cb-enable');
                    //jQuery(this).animate({backgroundPosition: -16}, "slow");
                    jQuery(this).fadeOut('fast', function(){
                        jQuery(this).css('backgroundPosition', '-16px 0');
                        jQuery(this).fadeIn('slow');
                                
                    });
                    
                    data['featured'] = 1;
                }
                for( var key in data){
                    post_data +="&"+key+"="+data[key];                        
                }
//                alert(post_data);
                jQuery.post(ajax_url, post_data,
                    function(response) {
                        stFacebookToolbox.sendMessage(response);                        
                        if(callbackFunction){
                            callbackFunction.call(this, data);
                        }
                    }
                    );
            });
        });
    }

};
        
function facebook_like_get_user_info(data,start,pageid){
    var limit = (parseInt(start)+500);
    percent = (limit/data)*100;
    number = (data > (start+500))?data:(start+500);

    jQuery("#facebook_get_fan_list").html("<img src='"+jQuery('#picture').val()+"' /><br />Updating "+(start+500)+" of "+data+" fan");
    //--------------------------
    var post_data = "ajax=1&action=get_facebook_user&id="+pageid+"&limit="+start;
    jQuery.ajax({
        type: "POST",
        data: post_data,
        cache: false,
        success: function(data){
            if(data == 'success')
            {
                if(limit < data)
                {
                    facebook_like_get_user_info(data,limit,pageid);
                }
                else
                {
                    stFacebookToolbox.loadPage("get_fan_list&pageid="+jQuery("#pageid").val(), "", "Get Fan Success");
                }
            }
                        
                        

        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            setTimeout(jQuery.unblockUI, 2000);
        }
    });



}



